import requests
import json
import warnings
from urllib3.exceptions import InsecureRequestWarning
from common.logging_handle import mylog


def request(method, url, headers, body=None):
    """
    POST/GET请求方法
    入参：method/url/headers/data
    出参：res
    """
    warnings.simplefilter('ignore', InsecureRequestWarning)
    try:
        mylog.info("请求类型----:" + method)
        mylog.info("请求链接-------:" + str(url))
        mylog.info("请求头部---:" + str(headers))
        mylog.info("请求内容------:" + str(body))
        if body == None:
            body = ""
        else:
            body = json.loads(body)
            # print(type(body))
        if method == 'GET':
            res = requests.get(url=url, headers=headers, timeout=10, verify=False)
        elif method == 'POST':
            res = requests.post(url=url, json=body, headers=headers, timeout=10, verify=False)
        elif method == 'DELETE':
            res = requests.delete(url=url, headers=headers,json=body,timeout=10, verify=False)
        elif method == 'PUT':
            res = requests.put(url=url, json=body,headers=headers, timeout=10, verify=False)
        else:
            raise NameError
        res = json.loads(res.content.decode('utf-8'))                     # 这里转成json
        mylog.info("返回信息------:" + str(res) + "\n--\n--\n--\n")
        return res
    except Exception as e:
        mylog.info("方法请求失败-" + str(e))

if __name__ == '__main__':
    pass
